package com.xln.furns.service.impl;

import com.xln.furns.dao.FurnDAO;
import com.xln.furns.dao.impl.FurnDAOimpl;
import com.xln.furns.entity.Furn;
import com.xln.furns.entity.Page;
import com.xln.furns.service.FurnService;

import java.util.List;

public class FurnServiceimpl implements FurnService {
    private FurnDAO furnDAO = new FurnDAOimpl();
    @Override
    public List<Furn> queryFurn() {

        return furnDAO.queryFurn();
    }

    @Override
    public int addFurn(Furn furn) {

        return furnDAO.addFurn(furn);
    }

    @Override
    public int deleteFurn(int id) {
        return furnDAO.deleteFurn(id);
    }

    @Override
    public Furn queryFurnById(int id) {


        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {

        return furnDAO.update(furn);
    }

    @Override
    public Page<Furn> page(Integer pageNo, Integer pageSize) {
//        public static final Integer PAGE_SIZE=3;
//        private Integer pageNo;//第几页
//        private Integer pageSize = PAGE_SIZE;//每页多少条记录
//        private Integer pageTotalConut;//总页数
//        private Integer totalRow;//数据库相应表总记录数
//        private List<T> items;
//        private String url;
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
        page.setTotalRow(furnDAO.getTotalRow());
        if (furnDAO.getTotalRow()%pageSize > 0){
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut+1);
        }else {
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut);
        }
        Integer begin = (pageNo - 1) * pageSize;
        List<Furn> pageItem = furnDAO.getPageItem(begin,pageSize);
        page.setItems(pageItem);
        return page;
    }

    @Override
    public Page<Furn> pageByname(int pageNo, int pageSize, String name) {

        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Integer pageTotalConut = furnDAO.getTotalRowByname(name)/pageSize;
        page.setTotalRow(furnDAO.getTotalRowByname(name));
        if (furnDAO.getTotalRowByname(name)%pageSize > 0){
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut+1);
        }else {
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut);
        }
        Integer begin = (pageNo - 1) * pageSize;
        List<Furn> pageItem = furnDAO.getPageItemsByname(begin,pageSize,name);
        page.setItems(pageItem);
        return page;

    }
}
